_require "../../basis.smi"
_require "./SQLBackendTy.smi"
_require "./Backend.smi"
_require local "../../smlformat-lib.smi"
_require local "./SQLErrors.smi"
_require "./builtintypes/timestamp/TimeStamp.smi"
_require "./SQLQuery.ppg.smi"
_require "./Numeric.smi"

structure SMLSharp_SQL_Prim =
struct

  structure Ty = SMLSharp_SQL_BackendTy
  structure Ast = SMLSharp_SQL_Query
  structure List = List
  structure Option = Option
  structure Bool = Bool
  structure Numeric = SMLSharp_SQL_Numeric

  datatype bool3 = True | False | Unknown
  type timestamp = SMLSharp_SQL_TimeStamp.timestamp
  type numeric = Numeric.num
  type decimal = numeric

  type backend = SMLSharp_SQL_Backend.backend
  type 'a server (= boxed)
  type 'a conn (= boxed)
  type 'a cursor (= boxed)
  type res (= boxed)
  exception Toy

  datatype ('toy, 'w) exp =
      EXPty of 'w Ast.exp_ast * 'toy
  datatype ('toy, 'w) whr =
      WHRty of 'w Ast.whr_ast * 'toy
  datatype ('toy, 'w) from =
      FROMty of 'w Ast.from_ast * ({} -> 'toy)
  datatype ('toy, 'w) orderby =
      ORDERBYty of 'w Ast.orderby_ast * 'toy
  datatype ('toy, 'w) offset =
      OFFSETty of 'w Ast.limit_ast * 'toy
  datatype ('toy, 'w) limit =
      LIMITty of 'w Ast.limit_ast * 'toy
  datatype ('src, 'toy, 'w) select =
      SELECTty of 'w Ast.select_ast * ('src -> 'toy) * (res -> 'toy)
  datatype ('toy, 'w) query =
      QUERYty of 'w Ast.query_ast * ({} -> 'toy) * (res -> 'toy)
  datatype ('toy, 'w) command =
      COMMANDty of 'w Ast.command_ast * ({} -> 'toy) * (res -> 'toy)
  datatype ('toy, 'w) db =
      DBty of 'w Ast.db * (unit -> 'toy)

  val closeRes : res -> unit
  val closeCommand : res -> unit

  val dummyCursor : 'a list -> 'a cursor
  val newCursor : (res -> 'a list) -> res -> 'a cursor
  val queryCommand : ('a list,'w) query -> ('a cursor,'w) command

  val sqlserver_string : string * (Ty.schema * (unit -> 'a)) -> 'a server
  val sqlserver_backend : backend * (Ty.schema * (unit -> 'a)) -> 'a server
  val sqlserver =
      case 'a in 'a * (Ty.schema * (unit -> 'b)) -> 'b server of
        string => sqlserver_string
      | backend => sqlserver_backend

  val sqleval : (('a,'w) db -> ('b,'w) command) -> 'a conn -> 'b

  val connect : 'a server -> 'a conn
  val connectAndCreate : 'a server -> 'a conn
  val closeConn : 'a conn -> unit
  val closeCursor : 'a cursor -> unit
  val fetch : 'a cursor -> 'a option
  val fetchAll : 'a cursor -> 'a list

  val toy : (('a,'w) db -> ('b,'w) query) -> 'a -> 'b
  val commandToString : (('a,'w) db -> ('b,'w) command) -> string
  val queryToString : (('a,'w) db -> ('b,'w) query) -> string
  val expToString : ('a,'w) exp -> string

  val fromSQL_int : res * int -> int
  val fromSQL_intInf : res * int -> intInf
  val fromSQL_word : res * int -> word
  val fromSQL_char : res * int -> char
  val fromSQL_bool : res * int -> bool
  val fromSQL_string : res * int -> string
  val fromSQL_real : res * int -> real
  val fromSQL_real32 : res * int -> real32
  val fromSQL_timestamp : res * int -> timestamp
  val fromSQL_numeric : res * int -> numeric
  val fromSQL_intOption : res * int -> int option
  val fromSQL_intInfOption : res * int -> intInf option
  val fromSQL_wordOption : res * int -> word option
  val fromSQL_charOption : res * int -> char option
  val fromSQL_boolOption : res * int -> bool option
  val fromSQL_stringOption : res * int -> string option
  val fromSQL_realOption : res * int -> real option
  val fromSQL_real32Option : res * int -> real32 option
  val fromSQL_timestampOption : res * int -> timestamp option
  val fromSQL_numericOption : res * int -> numeric option
  val fromSQL =
      case 'a in res * int -> 'a of
        int => fromSQL_int
      | intInf => fromSQL_intInf
      | word => fromSQL_word
      | char => fromSQL_char
      | bool => fromSQL_bool
      | string => fromSQL_string
      | real => fromSQL_real
      | real32 => fromSQL_real32
      | timestamp => fromSQL_timestamp
      | numeric => fromSQL_numeric
      | 'b option =>
        case 'b in res * int -> 'b option of
          int => fromSQL_intOption
        | intInf => fromSQL_intInfOption
        | word => fromSQL_wordOption
        | char => fromSQL_charOption
        | bool => fromSQL_boolOption
        | string => fromSQL_stringOption
        | real => fromSQL_realOption
        | real32 => fromSQL_real32Option
        | timestamp => fromSQL_timestampOption
        | numeric => fromSQL_numericOption

  val toSQL_int : int -> Ast.const
  val toSQL_intInf : intInf -> Ast.const
  val toSQL_word : word -> Ast.const
  val toSQL_char : char -> Ast.const
  val toSQL_bool : bool -> Ast.const
  val toSQL_string : string -> Ast.const
  val toSQL_real : real -> Ast.const
  val toSQL_real32 : real32 -> Ast.const
  val toSQL_timestamp : timestamp -> Ast.const
  val toSQL_numeric : numeric -> Ast.const
  val toSQL_intOption : int option -> Ast.const
  val toSQL_intInfOption : IntInf.int option -> Ast.const
  val toSQL_wordOption : word option -> Ast.const
  val toSQL_charOption : char option -> Ast.const
  val toSQL_boolOption : bool option -> Ast.const
  val toSQL_stringOption : string option -> Ast.const
  val toSQL_realOption : real option -> Ast.const
  val toSQL_real32Option : real32 option -> Ast.const
  val toSQL_timestampOption : timestamp option -> Ast.const
  val toSQL_numericOption : numeric option -> Ast.const
  val toSQL =
      case 'a in 'a -> Ast.const of
        int => toSQL_int
      | intInf => toSQL_intInf
      | word => toSQL_word
      | char => toSQL_char
      | bool => toSQL_bool
      | string => toSQL_string
      | real => toSQL_real
      | real32 => toSQL_real32
      | timestamp => toSQL_timestamp
      | numeric => toSQL_numeric
      | 'b option =>
        case 'b in 'b option -> Ast.const of
          int => toSQL_intOption
        | intInf => toSQL_intInfOption
        | word => toSQL_wordOption
        | char => toSQL_charOption
        | bool => toSQL_boolOption
        | string => toSQL_stringOption
        | real => toSQL_realOption
        | real32 => toSQL_real32Option
        | timestamp => toSQL_timestampOption
        | numeric => toSQL_numericOption

  val toyServer : unit -> 'a

  val ty_int : (unit -> int) -> Ty.schema_column
  val ty_intInf : (unit -> intInf) -> Ty.schema_column
  val ty_word : (unit -> word) -> Ty.schema_column
  val ty_char : (unit -> char) -> Ty.schema_column
  val ty_bool : (unit -> bool) -> Ty.schema_column
  val ty_string : (unit -> string) -> Ty.schema_column
  val ty_real : (unit -> real) -> Ty.schema_column
  val ty_real32 : (unit -> real32) -> Ty.schema_column
  val ty_timestamp : (unit -> timestamp) -> Ty.schema_column
  val ty_numeric : (unit -> numeric) -> Ty.schema_column
  val ty_intOption : (unit -> int option) -> Ty.schema_column
  val ty_intInfOption : (unit -> intInf option) -> Ty.schema_column
  val ty_wordOption : (unit -> word option) -> Ty.schema_column
  val ty_charOption : (unit -> char option) -> Ty.schema_column
  val ty_boolOption : (unit -> bool option) -> Ty.schema_column
  val ty_stringOption : (unit -> string option) -> Ty.schema_column
  val ty_realOption : (unit -> real option) -> Ty.schema_column
  val ty_real32Option : (unit -> real32 option) -> Ty.schema_column
  val ty_timestampOption : (unit -> timestamp option) -> Ty.schema_column
  val ty_numericOption : (unit -> numeric option) -> Ty.schema_column
  val ty =
      case 'a in (unit -> 'a) -> Ty.schema_column of
        int => ty_int
      | intInf => ty_intInf
      | word => ty_word
      | char => ty_char
      | bool => ty_bool
      | string => ty_string
      | real => ty_real
      | real32 => ty_real32
      | timestamp => ty_timestamp
      | numeric => ty_numeric
      | 'b option =>
        case 'b in (unit -> 'b option) -> Ty.schema_column of
          int => ty_intOption
        | intInf => ty_intInfOption
        | word => ty_wordOption
        | char => ty_charOption
        | bool => ty_boolOption
        | string => ty_stringOption
        | real => ty_realOption
        | real32 => ty_real32Option
        | timestamp => ty_timestampOption
        | numeric => ty_numericOption

  val compare_int = Int.compare
  val compare_intInf = IntInf.compare
  val compare_word = Word.compare
  val compare_char = Char.compare
  val compare_bool : bool * bool -> order
  val compare_string = String.compare
  val compare_real = Real.compare
  val compare_real32 = Real32.compare
  val compare_timestamp = SMLSharp_SQL_TimeStamp.compare
  val compare_numeric = SMLSharp_SQL_Numeric.compare
  val compare_intOption : int option * int option -> order
  val compare_intInfOption : intInf option * intInf option -> order
  val compare_wordOption : word option * word option -> order
  val compare_charOption : char option * char option -> order
  val compare_boolOption : bool option * bool option -> order
  val compare_stringOption : string option * string option -> order
  val compare_realOption : real option * real option -> order
  val compare_real32Option : real32 option * real32 option -> order
  val compare_timestampOption : timestamp option * timestamp option -> order
  val compare_numericOption : numeric option * numeric option -> order
  val compare =
      case 'a in 'a * 'a -> order of
        int => compare_int
      | intInf => compare_intInf
      | word => compare_word
      | char => compare_char
      | bool => compare_bool
      | string => compare_string
      | real => compare_real
      | real32 => compare_real32
      | timestamp => compare_timestamp
      | numeric => compare_numeric
      | 'b option =>
        case 'b in 'b option * 'b option -> order of
          int => compare_intOption
        | intInf => compare_intInfOption
        | word => compare_wordOption
        | char => compare_charOption
        | bool => compare_boolOption
        | string => compare_stringOption
        | real => compare_realOption
        | Real32.real => compare_real32Option
        | timestamp => compare_timestampOption
        | numeric => compare_numericOption

  structure General2 =
  struct
    val reverseOrder : order -> order
    val comparePair : ('a * 'a -> order) * ('b * 'b -> order)
                      -> ('a * 'b) * ('a * 'b) -> order
  end

  structure Bool3 =
  struct
    datatype bool3 = datatype bool3
    val fromBool : bool -> bool3
    val isTrue : bool3 -> bool
    val is : bool3 -> bool3 -> bool3
    val and3 : bool3 * bool3 -> bool3
    val or3 : bool3 * bool3 -> bool3
    val not3 : bool3 -> bool3
  end

  structure List2 =
  struct
    val onlyOne : {1:'a} list -> 'a
    val isNotEmpty : 'a list -> bool
    val prod : ('a * 'b) list * ('c * 'd) list
               -> ((('a * 'b) * ('c * 'd)) * ('b * 'd)) list
    val join : ('b * 'd -> 'e)
               -> ('a * 'b) list * ('c * 'd) list
               -> ((('a * 'b) * ('c * 'd)) * 'e) list
    val nub : ('a * 'a -> order) -> 'a list -> 'a list
    val sortBy : ('a -> 'b) -> ('b * 'b -> order) -> 'a list -> 'a list
    val groupBy : ('a -> 'b) -> ('b * 'b -> order) -> 'a list -> 'a list list
  end

  val add_intOption : int option * int option -> int option
  val add_intInfOption : intInf option * intInf option -> intInf option
  val add_wordOption : word option * word option -> word option
  val add_realOption : real option * real option -> real option
  val add_real32Option : real32 option * real32 option -> real32 option
  val add_numericOption : numeric option * numeric option -> numeric option

  val sub_intOption : int option * int option -> int option
  val sub_intInfOption : intInf option * intInf option -> intInf option
  val sub_wordOption : word option * word option -> word option
  val sub_realOption : real option * real option -> real option
  val sub_real32Option : real32 option * real32 option -> real32 option
  val sub_numericOption : numeric option * numeric option -> numeric option

  val mul_intOption : int option * int option -> int option
  val mul_intInfOption : intInf option * intInf option -> intInf option
  val mul_wordOption : word option * word option -> word option
  val mul_realOption : real option * real option -> real option
  val mul_real32Option : real32 option * real32 option -> real32 option
  val mul_numericOption : numeric option * numeric option -> numeric option

  val div_intOption : int option * int option -> int option
  val div_intInfOption : intInf option * intInf option -> intInf option
  val div_wordOption : word option * word option -> word option
  val div_realOption : real option * real option -> real option
  val div_real32Option : real32 option * real32 option -> real32 option
  val div_numericOption : numeric option * numeric option -> numeric option

  val mod_real : real * real -> real
  val mod_real32 : real32 * real32 -> real32
  val mod_intOption : int option * int option -> int option
  val mod_intInfOption : intInf option * intInf option -> intInf option
  val mod_wordOption : word option * word option -> word option
  val mod_realOption : real option * real option -> real option
  val mod_real32Option : real32 option * real32 option -> real32 option
  val mod_numericOption : numeric option * numeric option -> numeric option

  val neg_intOption : int option -> int option
  val neg_intInfOption : intInf option -> intInf option
  val neg_wordOption : word option -> word option
  val neg_realOption : real option -> real option
  val neg_real32Option : real32 option -> real32 option
  val neg_numericOption : numeric option -> numeric option

  val abs_word : word -> word
  val abs_intOption : int option -> int option
  val abs_intInfOption : intInf option -> intInf option
  val abs_wordOption : word option -> word option
  val abs_realOption : real option -> real option
  val abs_real32Option : real32 option -> real32 option
  val abs_numericOption : numeric option -> numeric option

  val lt_int : int * int -> bool3
  val lt_intInf : intInf * intInf -> bool3
  val lt_word : word * word -> bool3
  val lt_char : char * char -> bool3
  val lt_bool : bool * bool -> bool3
  val lt_string : string * string -> bool3
  val lt_real : real * real -> bool3
  val lt_real32 : real32 * real32 -> bool3
  val lt_timestamp : timestamp * timestamp -> bool3
  val lt_numeric : numeric * numeric -> bool3
  val lt_intOption : int option * int option -> bool3
  val lt_intInfOption : intInf option * intInf option -> bool3
  val lt_wordOption : word option * word option -> bool3
  val lt_charOption : char option * char option -> bool3
  val lt_boolOption : bool option * bool option -> bool3
  val lt_stringOption : string option * string option -> bool3
  val lt_realOption : real option * real option -> bool3
  val lt_real32Option : real32 option * real32 option -> bool3
  val lt_timestampOption : timestamp option * timestamp option -> bool3
  val lt_numericOption : numeric option * numeric option -> bool3

  val gt_int : int * int -> bool3
  val gt_intInf : intInf * intInf -> bool3
  val gt_word : word * word -> bool3
  val gt_char : char * char -> bool3
  val gt_bool : bool * bool -> bool3
  val gt_string : string * string -> bool3
  val gt_real : real * real -> bool3
  val gt_real32 : real32 * real32 -> bool3
  val gt_timestamp : timestamp * timestamp -> bool3
  val gt_numeric : numeric * numeric -> bool3
  val gt_intOption : int option * int option -> bool3
  val gt_intInfOption : intInf option * intInf option -> bool3
  val gt_wordOption : word option * word option -> bool3
  val gt_charOption : char option * char option -> bool3
  val gt_boolOption : bool option * bool option -> bool3
  val gt_stringOption : string option * string option -> bool3
  val gt_realOption : real option * real option -> bool3
  val gt_real32Option : real32 option * real32 option -> bool3
  val gt_timestampOption : timestamp option * timestamp option -> bool3
  val gt_numericOption : numeric option * numeric option -> bool3

  val le_int : int * int -> bool3
  val le_intInf : intInf * intInf -> bool3
  val le_word : word * word -> bool3
  val le_char : char * char -> bool3
  val le_bool : bool * bool -> bool3
  val le_string : string * string -> bool3
  val le_real : real * real -> bool3
  val le_real32 : real32 * real32 -> bool3
  val le_timestamp : timestamp * timestamp -> bool3
  val le_numeric : numeric * numeric -> bool3
  val le_intOption : int option * int option -> bool3
  val le_intInfOption : intInf option * intInf option -> bool3
  val le_wordOption : word option * word option -> bool3
  val le_charOption : char option * char option -> bool3
  val le_boolOption : bool option * bool option -> bool3
  val le_stringOption : string option * string option -> bool3
  val le_realOption : real option * real option -> bool3
  val le_real32Option : real32 option * real32 option -> bool3
  val le_timestampOption : timestamp option * timestamp option -> bool3
  val le_numericOption : numeric option * numeric option -> bool3

  val ge_int : int * int -> bool3
  val ge_intInf : intInf * intInf -> bool3
  val ge_word : word * word -> bool3
  val ge_char : char * char -> bool3
  val ge_bool : bool * bool -> bool3
  val ge_string : string * string -> bool3
  val ge_real : real * real -> bool3
  val ge_real32 : real32 * real32 -> bool3
  val ge_timestamp : timestamp * timestamp -> bool3
  val ge_numeric : numeric * numeric -> bool3
  val ge_intOption : int option * int option -> bool3
  val ge_intInfOption : intInf option * intInf option -> bool3
  val ge_wordOption : word option * word option -> bool3
  val ge_charOption : char option * char option -> bool3
  val ge_boolOption : bool option * bool option -> bool3
  val ge_stringOption : string option * string option -> bool3
  val ge_realOption : real option * real option -> bool3
  val ge_real32Option : real32 option * real32 option -> bool3
  val ge_timestampOption : timestamp option * timestamp option -> bool3
  val ge_numericOption : numeric option * numeric option -> bool3

  val eq_int : int * int -> bool3
  val eq_intInf : intInf * intInf -> bool3
  val eq_word : word * word -> bool3
  val eq_char : char * char -> bool3
  val eq_bool : bool * bool -> bool3
  val eq_string : string * string -> bool3
  val eq_real : real * real -> bool3
  val eq_real32 : real32 * real32 -> bool3
  val eq_timestamp : timestamp * timestamp -> bool3
  val eq_numeric : numeric * numeric -> bool3
  val eq_intOption : int option * int option -> bool3
  val eq_intInfOption : intInf option * intInf option -> bool3
  val eq_wordOption : word option * word option -> bool3
  val eq_charOption : char option * char option -> bool3
  val eq_boolOption : bool option * bool option -> bool3
  val eq_stringOption : string option * string option -> bool3
  val eq_realOption : real option * real option -> bool3
  val eq_real32Option : real32 option * real32 option -> bool3
  val eq_timestampOption : timestamp option * timestamp option -> bool3
  val eq_numericOption : numeric option * numeric option -> bool3

  val neq_int : int * int -> bool3
  val neq_intInf : intInf * intInf -> bool3
  val neq_word : word * word -> bool3
  val neq_char : char * char -> bool3
  val neq_bool : bool * bool -> bool3
  val neq_string : string * string -> bool3
  val neq_real : real * real -> bool3
  val neq_real32 : real32 * real32 -> bool3
  val neq_timestamp : timestamp * timestamp -> bool3
  val neq_numeric : numeric * numeric -> bool3
  val neq_intOption : int option * int option -> bool3
  val neq_intInfOption : intInf option * intInf option -> bool3
  val neq_wordOption : word option * word option -> bool3
  val neq_charOption : char option * char option -> bool3
  val neq_boolOption : bool option * bool option -> bool3
  val neq_stringOption : string option * string option -> bool3
  val neq_realOption : real option * real option -> bool3
  val neq_real32Option : real32 option * real32 option -> bool3
  val neq_timestampOption : timestamp option * timestamp option -> bool3
  val neq_numericOption : numeric option * numeric option -> bool3

  val concat_stringOption : string option * string option -> string option

  val like_string : string * string -> bool3
  val like_stringOption : string option * string option -> bool3

  val nullif_intOption : int option * int option -> int option
  val nullif_intInfOption : intInf option * intInf option -> intInf option
  val nullif_wordOption : word option * word option -> word option
  val nullif_charOption : char option * char option -> char option
  val nullif_boolOption : bool option * bool option -> bool option
  val nullif_stringOption : string option * string option -> string option
  val nullif_realOption : real option * real option -> real option
  val nullif_real32Option : real32 option * real32 option -> real32 option
  val nullif_timestampOption
      : timestamp option * timestamp option -> timestamp option
  val nullif_numericOption : numeric option * numeric option -> numeric option

  val avg_int : int list -> numeric option
  val avg_intInf : intInf list -> numeric option
  val avg_word : word list -> numeric option
  val avg_real : real list -> numeric option
  val avg_real32 : real32 list -> numeric option
  val avg_numeric : numeric list -> numeric option
  val avg_intOption : int option list -> numeric option
  val avg_intInfOption : intInf option list -> numeric option
  val avg_wordOption : word option list -> numeric option
  val avg_realOption : real option list -> numeric option
  val avg_real32Option : real32 option list -> numeric option
  val avg_numericOption : numeric option list -> numeric option

  val sum_int : int list -> int option
  val sum_intInf : intInf list -> intInf option
  val sum_word : word list -> word option
  val sum_real : real list -> real option
  val sum_real32 : real32 list -> real32 option
  val sum_numeric : numeric list -> numeric option
  val sum_intOption : int option list -> int option
  val sum_intInfOption : intInf option list -> intInf option
  val sum_wordOption : word option list -> word option
  val sum_realOption : real option list -> real option
  val sum_real32Option : real32 option list -> real32 option
  val sum_numericOption : numeric option list -> numeric option

  val max_int : int list -> int option
  val max_intInf : intInf list -> intInf option
  val max_word : word list -> word option
  val max_char : char list -> char option
  val max_bool : bool list -> bool option
  val max_string : string list -> string option
  val max_real : real list -> real option
  val max_real32 : real32 list -> real32 option
  val max_timestamp : timestamp list -> timestamp option
  val max_numeric : numeric list -> numeric option
  val max_intOption : int option list -> int option
  val max_intInfOption : intInf option list -> intInf option
  val max_wordOption : word option list -> word option
  val max_charOption : char option list -> char option
  val max_boolOption : bool option list -> bool option
  val max_stringOption : string option list -> string option
  val max_realOption : real option list -> real option
  val max_real32Option : real32 option list -> real32 option
  val max_timestampOption : timestamp option list -> timestamp option
  val max_numericOption : numeric option list -> numeric option

  val min_int : int list -> int option
  val min_intInf : intInf list -> intInf option
  val min_word : word list -> word option
  val min_char : char list -> char option
  val min_bool : bool list -> bool option
  val min_string : string list -> string option
  val min_real : real list -> real option
  val min_real32 : real32 list -> real32 option
  val min_timestamp : timestamp list -> timestamp option
  val min_numeric : numeric list -> numeric option
  val min_intOption : int option list -> int option
  val min_intInfOption : intInf option list -> intInf option
  val min_wordOption : word option list -> word option
  val min_charOption : char option list -> char option
  val min_boolOption : bool option list -> bool option
  val min_stringOption : string option list -> string option
  val min_realOption : real option list -> real option
  val min_real32Option : real32 option list -> real32 option
  val min_timestampOption : timestamp option list -> timestamp option
  val min_numericOption : numeric option list -> numeric option

  val count : 'a list -> int
  val count_option : 'a option list -> int

  val Num_int : int -> numeric option
  val Num_intInf : intInf -> numeric option
  val Num_word : word -> numeric option
  val Num_real : real -> numeric option
  val Num_real32 : real32 -> numeric option
  val Num_numeric : numeric -> numeric option
  val Num_intOption : int option -> numeric option
  val Num_intInfOption : intInf option -> numeric option
  val Num_wordOption : word option -> numeric option
  val Num_realOption : real option -> numeric option
  val Num_real32Option : real32 option -> numeric option
  val Num_numericOption : numeric option -> numeric option

  structure Op =
  struct

    val + =
        case 'a in 'a * 'a -> 'a of
          int => Int.+
        | intInf => IntInf.+
        | word => Word.+
        | real => Real.+
        | real32 => Real32.+
        | numeric => Numeric.+
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => add_intOption
          | intInf => add_intInfOption
          | word => add_wordOption
          | real => add_realOption
          | real32 => add_real32Option
          | numeric => add_numericOption

    val - =
        case 'a in 'a * 'a -> 'a of
          int => Int.-
        | intInf => IntInf.-
        | word => Word.-
        | real => Real.-
        | real32 => Real32.-
        | numeric => Numeric.-
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => sub_intOption
          | intInf => sub_intInfOption
          | word => sub_wordOption
          | real => sub_realOption
          | real32 => sub_real32Option
          | numeric => sub_numericOption

    val * =
        case 'a in 'a * 'a -> 'a of
          int => Int.*
        | intInf => IntInf.*
        | word => Word.*
        | real => Real.*
        | real32 => Real32.*
        | numeric => Numeric.*
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => mul_intOption
          | intInf => mul_intInfOption
          | word => mul_wordOption
          | real => mul_realOption
          | real32 => mul_real32Option
          | numeric => mul_numericOption

    val / =
        case 'a in 'a * 'a -> 'a of
          int => Int.quot
        | intInf => IntInf.quot
        | word => Word.div
        | real => Real./
        | real32 => Real32./
        | numeric => Numeric.quot
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => div_intOption
          | intInf => div_intInfOption
          | word => div_wordOption
          | real => div_realOption
          | real32 => div_real32Option
          | numeric => div_numericOption

    val mod =
        case 'a in 'a * 'a -> 'a of
          int => Int.rem
        | intInf => IntInf.rem
        | word => Word.mod
        | real => mod_real
        | real32 => mod_real32
        | numeric => Numeric.rem
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => mod_intOption
          | intInf => mod_intInfOption
          | word => mod_wordOption
          | real => mod_realOption
          | real32 => mod_real32Option
          | numeric => mod_numericOption

    val % =
        case 'a in 'a * 'a -> 'a of
          int => Int.rem
        | intInf => IntInf.rem
        | word => Word.mod
        | real => mod_real
        | real32 => mod_real32
        | numeric => Numeric.rem
        | 'b option =>
          case 'b in 'b option * 'b option -> 'b option of
            int => mod_intOption
          | intInf => mod_intInfOption
          | word => mod_wordOption
          | real => mod_realOption
          | real32 => mod_real32Option
          | numeric => mod_numericOption

    val ~ =
        case 'a in 'a -> 'a of
          int => Int.~
        | intInf => IntInf.~
        | word => Word.~
        | real => Real.~
        | real32 => Real32.~
        | numeric => Numeric.~
        | 'b option =>
          case 'b in 'b option -> 'b option of
            int => neg_intOption
          | intInf => neg_intInfOption
          | word => neg_wordOption
          | real => neg_realOption
          | real32 => neg_real32Option
          | numeric => neg_numericOption

    val abs =
        case 'a in 'a -> 'a of
          int => Int.abs
        | intInf => IntInf.abs
        | word => abs_word
        | real => Real.abs
        | real32 => Real32.abs
        | numeric => Numeric.abs
        | 'b option =>
          case 'b in 'b option -> 'b option of
            int => abs_intOption
          | intInf => abs_intInfOption
          | word => abs_wordOption
          | real => abs_realOption
          | real32 => abs_real32Option
          | numeric => abs_numericOption

    val < =
        case 'a in 'a * 'a -> bool3 of
          int => lt_int
        | intInf => lt_intInf
        | word => lt_word
        | char => lt_char
        | bool => lt_bool
        | real => lt_real
        | real32 => lt_real32
        | string => lt_string
        | timestamp => lt_timestamp
        | numeric => lt_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => lt_intOption
          | intInf => lt_intInfOption
          | word => lt_wordOption
          | char => lt_charOption
          | bool => lt_boolOption
          | real => lt_realOption
          | real32 => lt_real32Option
          | string => lt_stringOption
          | timestamp => lt_timestampOption
          | numeric => lt_numericOption

    val > =
        case 'a in 'a * 'a -> bool3 of
          int => gt_int
        | intInf => gt_intInf
        | word => gt_word
        | char => gt_char
        | bool => gt_bool
        | real => gt_real
        | real32 => gt_real32
        | string => gt_string
        | timestamp => gt_timestamp
        | numeric => gt_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => gt_intOption
          | intInf => gt_intInfOption
          | word => gt_wordOption
          | char => gt_charOption
          | bool => gt_boolOption
          | real => gt_realOption
          | real32 => gt_real32Option
          | string => gt_stringOption
          | timestamp => gt_timestampOption
          | numeric => gt_numericOption

    val <= =
        case 'a in 'a * 'a -> bool3 of
          int => le_int
        | intInf => le_intInf
        | word => le_word
        | char => le_char
        | bool => le_bool
        | real => le_real
        | real32 => le_real32
        | string => le_string
        | timestamp => le_timestamp
        | numeric => le_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => le_intOption
          | intInf => le_intInfOption
          | word => le_wordOption
          | char => le_charOption
          | bool => le_boolOption
          | real => le_realOption
          | real32 => le_real32Option
          | string => le_stringOption
          | timestamp => le_timestampOption
          | numeric => le_numericOption

    val >= =
        case 'a in 'a * 'a -> bool3 of
          int => ge_int
        | intInf => ge_intInf
        | word => ge_word
        | char => ge_char
        | bool => ge_bool
        | real => ge_real
        | real32 => ge_real32
        | string => ge_string
        | timestamp => ge_timestamp
        | numeric => ge_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => ge_intOption
          | intInf => ge_intInfOption
          | word => ge_wordOption
          | char => ge_charOption
          | bool => ge_boolOption
          | real => ge_realOption
          | real32 => ge_real32Option
          | string => ge_stringOption
          | timestamp => ge_timestampOption
          | numeric => ge_numericOption

    val = =
        case 'a in 'a * 'a -> bool3 of
          int => eq_int
        | intInf => eq_intInf
        | word => eq_word
        | char => eq_char
        | bool => eq_bool
        | real => eq_real
        | real32 => eq_real32
        | string => eq_string
        | timestamp => eq_timestamp
        | numeric => eq_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => eq_intOption
          | intInf => eq_intInfOption
          | word => eq_wordOption
          | char => eq_charOption
          | bool => eq_boolOption
          | real => eq_realOption
          | real32 => eq_real32Option
          | string => eq_stringOption
          | timestamp => eq_timestampOption
          | numeric => eq_numericOption

    val <> =
        case 'a in 'a * 'a -> bool3 of
          int => neq_int
        | intInf => neq_intInf
        | word => neq_word
        | char => neq_char
        | bool => neq_bool
        | real => neq_real
        | real32 => neq_real32
        | string => neq_string
        | timestamp => neq_timestamp
        | numeric => neq_numeric
        | 'b option =>
          case 'b in 'b option * 'b option -> bool3 of
            int => neq_intOption
          | intInf => neq_intInfOption
          | word => neq_wordOption
          | char => neq_charOption
          | bool => neq_boolOption
          | real => neq_realOption
          | real32 => neq_real32Option
          | string => neq_stringOption
          | timestamp => neq_timestampOption
          | numeric => neq_numericOption

    val || =
        case 'a in 'a * 'a -> 'a of
          string => String.^
        | string option => concat_stringOption

    val like =
        case 'a in 'a * 'a -> bool3 of
          string => like_string
        | string option => like_stringOption

    val nullif =
        case 'a in 'a option * 'a option -> 'a option of
          int => nullif_intOption
        | intInf => nullif_intInfOption
        | word => nullif_wordOption
        | char => nullif_charOption
        | bool => nullif_boolOption
        | real => nullif_realOption
        | real32 => nullif_real32Option
        | string => nullif_stringOption
        | timestamp => nullif_timestampOption
        | numeric => nullif_numericOption

    val coalesce : 'a option * 'a -> 'a
    val coalesce' : 'a option * 'a option -> 'a option

    val avg =
        case 'a in 'a list -> numeric option of
          int => avg_int
        | intInf => avg_intInf
        | word => avg_word
        | real => avg_real
        | real32 => avg_real32
        | numeric => avg_numeric
        | 'b option =>
          case 'b in 'b option list -> numeric option of
            int => avg_intOption
          | intInf => avg_intInfOption
          | word => avg_wordOption
          | real => avg_realOption
          | real32 => avg_real32Option
          | numeric => avg_numericOption

    val sum =
        case 'a in 'a list -> 'a option of
          int => sum_int
        | intInf => sum_intInf
        | word => sum_word
        | real => sum_real
        | real32 => sum_real32
        | numeric => sum_numeric

    val sum' =
        case 'a in 'a option list -> 'a option of
          int => sum_intOption
        | intInf => sum_intInfOption
        | word => sum_wordOption
        | real => sum_realOption
        | real32 => sum_real32Option
        | numeric => sum_numericOption

    val max =
        case 'a in 'a list -> 'a option of
          int => max_int
        | intInf => max_intInf
        | word => max_word
        | char => max_char
        | bool => max_bool
        | real => max_real
        | real32 => max_real32
        | string => max_string
        | timestamp => max_timestamp
        | numeric => max_numeric

    val max' =
        case 'a in 'a option list -> 'a option of
          int => max_intOption
        | intInf => max_intInfOption
        | word => max_wordOption
        | char => max_charOption
        | bool => max_boolOption
        | real => max_realOption
        | real32 => max_real32Option
        | string => max_stringOption
        | timestamp => max_timestampOption
        | numeric => max_numericOption

    val min =
        case 'a in 'a list -> 'a option of
          int => min_int
        | intInf => min_intInf
        | word => min_word
        | char => min_char
        | bool => min_bool
        | real => min_real
        | real32 => min_real32
        | string => min_string
        | timestamp => min_timestamp
        | numeric => min_numeric

    val min' =
        case 'a in 'a option list -> 'a option of
          int => min_intOption
        | intInf => min_intInfOption
        | word => min_wordOption
        | char => min_charOption
        | bool => min_boolOption
        | real => min_realOption
        | real32 => min_real32Option
        | string => min_stringOption
        | timestamp => min_timestampOption
        | numeric => min_numericOption

    val count =
        case 'a in 'a list -> int of
          int => count
        | intInf => count
        | word => count
        | char => count
        | bool => count
        | real => count
        | real32 => count
        | string => count
        | timestamp => count
        | numeric => count
        | 'b option =>
          case 'b in 'b option list -> int of
            int => count_option
          | intInf => count_option
          | word => count_option
          | char => count_option
          | bool => count_option
          | real => count_option
          | real32 => count_option
          | string => count_option
          | timestamp => count_option
          | numeric => count_option

    val Num =
        case 'a in 'a -> numeric option of
          int => Num_int
        | intInf => Num_intInf
        | word => Num_word
        | real => Num_real
        | real32 => Num_real32
        | numeric => Num_numeric
        | 'b option =>
          case 'b in 'b option -> numeric option of
            int => Num_intOption
          | intInf => Num_intInfOption
          | word => Num_wordOption
          | real => Num_realOption
          | real32 => Num_real32Option
          | numeric => Num_numericOption

    val Some : 'a -> 'a option
    val Part : 'a option list -> 'a list

  end

end
